<?php
/**
 * 			Plugin editors Switcher
 * @version		1.0.1
 * @copyright		Copyright (C) 2009 Joomler!.net. All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @author		Joomler!.net  joomlers@gmail.com
 * @link			http://www.joomler.net
 */

/**
* @package		Joomla
* @copyright		Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgEditorSwitcher extends JPlugin
{

	var $_switchereditor = null;

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param 	object $subject The object to observe
	 * @param 	array  $config  An array that holds the plugin configuration
	 * @since 1.5
	 */
	function plgEditorSwitcher(& $subject, $config)
	{
		$mainframe = & JFactory::getApplication();
		$switcher = JPluginHelper::getPlugin('editors', 'switcher');

		$editor = strtolower( $mainframe->getUserStateFromRequest('editorswitcher.editor', 'switcheditor', $switcher->params->get('default_editor', 'tinymce'), 'cmd') );
		if($editor == 'switcher'){
			$editor = $switcher->params->get('default_editor', 'none');
		}

		if(file_exists(dirname(__FILE__). DS. $editor. '.php') && JPluginHelper::isEnabled('editors', $editor)){
			$plugin = JPluginHelper::getPlugin('editors', $editor);
			$this->_setSwitcherEditor($subject, $config, $plugin);
		} else {
			$plugins = JPluginHelper::getPlugin('editors');
			$plugin = null;
			if(count($plugins)){
				foreach($plugins as $v){
					if($v->name != 'switcher'){
						$plugin = $v;
						break;
					}
				}
			}

			if($plugin){
				$this->_setSwitcherEditor($subject, $config, $plugin);
			} else {
				JError::raiseWarning('SOME_ERROR_CODE', JText::_('Editor was not found.'));
			}
		}

		parent::__construct($subject, $config);
	}

	function _setSwitcherEditor(& $subject, & $config, $plugin)
	{
		$editor = $plugin->name;

		$config['params'] = $plugin->params;
		$config['name'] = strtolower($editor);
		$config['type'] = 'editor';
		require_once dirname(__FILE__). DS. $editor. '.php';
		$classname = 'plgEditor'. ucfirst($editor);
		$this->_switchereditor = new $classname($subject, $config);
	}

	function _setEditorSelector($current)
	{
		static $output = null;

		$selector = '';

		if(is_null($output)){
			$output = true;
			$db		= & JFactory::getDBO();
			$user	= & JFactory::getUser();

			$groups = $this->params->get('switcher_allowed_groups', array(20, 21, 23, 24, 25));
			if(is_numeric($groups)){
				$groups = array($groups);
			}

			if(!in_array($user->get('gid'), $groups) ) {
				return '';
			}

			// compile list of the editors
			$query = 'SELECT element AS value, name AS text'
			. ' FROM #__plugins'
			. ' WHERE folder = "editors"'
			. ' AND published = 1 AND element <> "switcher"'
			. ' ORDER BY ordering, name'
			;
			$db->setQuery( $query );
			$editors = $db->loadObjectList();

			$uri = JURI::current();

			$action = $uri . (strpos('?', $uri)? '&':'?'). $this->_buildAction();
			$task = JRequest::getCmd('task');
			$section = JRequest::getCmd('section');

			JHTML::_('behavior.mootools');

			$js = 'onchange="if($(\'section\')) new Element(\'input\').setProperties({\'type\':\'hidden\',\'name\':\'section\',\'value\':\''. $section.'\'}).inject(document.adminForm);document.adminForm.task.value=\''. $task. '\';document.adminForm.action=\''. $action. '\';document.adminForm.submit();"';

			$selector = '<div id="switcherSelector" style="float:right;margin-top:3px;">'
			. JHTML::_('select.genericlist',   $editors, 'switcheditor'. '', 'class="modal-button" '. $js, 'value', 'text', $current, 'jswitcheditor' ). '</div>';

			$document = & JFactory::getDocument();
			$js = "window.addEvent('domready', function(){if($('editor-xtd-buttons')) $('switcherSelector').inject($('editor-xtd-buttons'));});";
			$document->addScriptDeclaration($js);
		}

		return $selector;
	}

	function _buildAction()
	{
		/**
		 * Depending on the extension which you use, it may be necessary to add a parameter.
		 */

		$action = array();
		$vars = array(
			'option',
			'view',
			'task',
			'section',
			'sectionid',
			'cid[]',
			'type',
			'scope',
			'tmpl',
		);

		foreach($vars as $var){
			$value = null;
			if(strpos($var, '[]')){
				$temp = JRequest::getVar(str_replace('[]', '', $var), array(), '', 'array');
				if(count($temp)){
					$value = $temp[0];
				}
			} else {
				$value = JRequest::getCmd($var);
			}

			if($value){
				$action[] = $var. '='. $value;
			}

		}

		return implode('&', $action);
	}

	function onInit()
	{
		if(is_object($this->_switchereditor)){
			return $this->_switchereditor->onInit();
		}
	}

	function onGetContent( $editor ) {
		if(is_object($this->_switchereditor)){
			return $this->_switchereditor->onGetContent($editor);
		}
	}

	function onSetContent( $editor, $html ) {
		if(is_object($this->_switchereditor)){
			return $this->_switchereditor->onSetContent( $editor, $html );
		}
	}

	function onSave( $editor ) {
		if(is_object($this->_switchereditor)){
			return $this->_switchereditor->onSave( $editor );
		}
	}

	function onDisplay( $name, $content, $width, $height, $col, $row, $buttons = true)
	{
		if(is_object($this->_switchereditor)){
			$selector = $this->_setEditorSelector($this->_switchereditor->_name);
			return $this->_switchereditor->onDisplay( $name, $content, $width, $height, $col, $row, $buttons). $selector;
		}
	}

	function onGetInsertMethod($name)
	{
		if(is_object($this->_switchereditor)){
			return $this->_switchereditor->onGetInsertMethod($name);
		}
	}
}
